<?php
// +----------------------------------------------------------------------
// | [RhaPHP System] Copyright (c) 2017 http://www.rhaphp.com/
// +----------------------------------------------------------------------
// | [RhaPHP] 并不是自由软件,你可免费使用,未经许可不能去掉RhaPHP相关版权
// +----------------------------------------------------------------------
// | Author: Geeson <qimengkeji@vip.qq.com>
// +----------------------------------------------------------------------


namespace app\admin\controller;

use think\exception\ErrorException;
use think\facade\Cache;
use think\Db;
use think\facade\Request;


class Statistics extends Base
{

    public function initialize()
    {

        parent::initialize();

    }
    public function userStatistics(){

        $max_date = date('Y-m-d',strtotime('-1 day'));
        $this->assign('max_date',$max_date);
        if (Request::isGet() ) {
            $_data = input('get.');
           if(isset($_data['times']) && $_data['times']){
                $times = explode('到', $_data['times']);
                $start_date = trim($times[0]);
                $end_date = trim($times[1]);
                if(strtotime($end_date) > strtotime(date('Y-m-d'))){
                    $end_date = date('Y-m-d',strtotime('-1 day'));
                }
                  if(strtotime($start_date) > strtotime($end_date)){
                    $start_date = $end_date;
                }

               if((intval(strtotime($end_date) - strtotime($start_date))/86400) > 59){
                    $start_date = date('Y-m-d',strtotime($end_date.'-59 days'));
                }
            }else{
                $end_date = date('Y-m-d',strtotime('-1 day'));
                $start_date = date('Y-m-d',strtotime($end_date.'-13 days'));

            }
        }else{
            $end_date = date('Y-m-d',strtotime('-1 day'));
            $start_date = date('Y-m-d',strtotime($end_date.'-13 days'));

        }
        $time_region = 7;
        $times = $start_date." 到 ".$end_date;
        $sh['times']= $times;
        $mpList = $this->getRoleMpList();
        $legend = array();
        foreach ($mpList as $k=>$item) {
            $legend[] = $item['name'];
        }

        $end_time = strtotime($end_date);
        $start_time = strtotime($start_date);
        $allxAxis = array();
        $series = array();

        $date_count = 0;

        while ($start_time<=$end_time){
            $date_count++;
            $date = date('Y-m-d',$start_time);
            $allxAxis[] = $date;
            $start_time = $start_time + 86400;

        }

        $default_data = array();
        foreach ($allxAxis as $allxAxi) {
            $default_data[$allxAxi] = array();
        }
        foreach ($mpList as $item) {
            $region_count = intval($date_count/$time_region);
            $other_count = intval($date_count%$time_region);
            $now_start_date = $start_date;

            $wxObj = getWechatActiveObj($item['id']);

            $series[$item['id']] = array(
                'name'=>$item['name'],
                'type'=>'line',
                'stack'=>'总量',
                'data'=>$default_data
            );


            for(;$region_count>0;$region_count--){
                $api_start_date = $now_start_date;
                $api_end_date = date('Y-m-d',strtotime($now_start_date.'+'.($time_region-1).' days'));
                $now_start_date = date('Y-m-d',strtotime($api_end_date.'+1 day'));

                $result = $wxObj->getDatacube('user','summary',$api_start_date,$api_end_date);

                if(is_array($result)){
                    foreach ($result as $rs) {
                        $series[$item['id']]['data'][$rs['ref_date']][] = $rs;
                    }
                }

            }
            if($other_count>1){
                $api_start_date = $now_start_date;
                $api_end_date = date('Y-m-d',strtotime($now_start_date.'+'.($other_count-1).' days'));
                $result = $wxObj->getDatacube('user','summary',$api_start_date,$api_end_date);

                if(isset($result) && is_array($result)){
                    foreach ($result as $rs) {
                        $series[$item['id']]['data'][$rs['ref_date']][] = $rs;
                    }
                }
            }
        }

        $series1 = array();
        foreach ($series as $k=>$item) {
            $series_item = array(
                'name'=>$item['name'],
                'type'=>'line',
                'stack'=>'总量',
                'data'=>array()
            );
            foreach ($item['data'] as $datums) {
                $num = 0;
                if($datums){
                    $num = 0;
                    foreach ($datums as $datum) {
                         $num += $datum['new_user'];
                    }

                }
                $series_item['data'][] = $num;

            }
            $series1[] = $series_item;
        }


        $this->assign('total_title','新增人数统计');
        $this->assign('sh',$sh);
        $this->assign('legend',json_encode($legend));
        $this->assign('xAxis',json_encode($allxAxis));
        $this->assign('series',json_encode($series1));
        return view('userstatistics');
    }

    public function readStatistics(){
        $max_date = date('Y-m-d',strtotime('-1 day'));
        $this->assign('max_date',$max_date);
        if (Request::isGet() ) {
            $_data = input('get.');
           if(isset($_data['times']) && $_data['times']){
                $times = explode('到', $_data['times']);
                $start_date = trim($times[0]);
                $end_date = trim($times[1]);
                if(strtotime($end_date) > strtotime(date('Y-m-d'))){
                    $end_date = date('Y-m-d',strtotime('-1 day'));
                }
                  if(strtotime($start_date) > strtotime($end_date)){
                    $start_date = $end_date;
                }
               if((intval(strtotime($end_date) - strtotime($start_date))/86400) > 59){
                    $start_date = date('Y-m-d',strtotime($end_date.'-59 days'));
                }
            }else{
                $end_date = date('Y-m-d',strtotime('-1 day'));
                $start_date = date('Y-m-d',strtotime($end_date.'-13 days'));

            }
        }else{
            $end_date = date('Y-m-d',strtotime('-1 day'));
            $start_date = date('Y-m-d',strtotime($end_date.'-13 days'));

        }
        $times = $start_date." 到 ".$end_date;
        $sh['times']= $times;
        $mpList = $this->getRoleMpList();
        $legend = array();
        foreach ($mpList as $k=>$item) {
            $legend[] = $item['name'];
        }

        $end_time = strtotime($end_date);
        $start_time = strtotime($start_date);
        $allxAxis = array();
        $series = array();

        $date_count = 0;

        while ($start_time<=$end_time){
            $date_count++;
            $date = date('Y-m-d',$start_time);
            $allxAxis[] = $date;
            $start_time = $start_time + 86400;

        }

        $time_region = 3;
        $default_data = array();
        foreach ($allxAxis as $allxAxi) {
            $default_data[$allxAxi] = array();
        }
        foreach ($mpList as $item) {
            $region_count = intval($date_count/$time_region);
            $other_count = intval($date_count%$time_region);
            $now_start_date = $start_date;

            $wxObj = getWechatActiveObj($item['id']);

            $series[$item['id']] = array(
                'name'=>$item['name'],
                'type'=>'line',
                'stack'=>'总量',
                'data'=>$default_data
            );


            for(;$region_count>0;$region_count--){
                $api_start_date = $now_start_date;
                $api_end_date = date('Y-m-d',strtotime($now_start_date.'+'.($time_region-1).' days'));
                $now_start_date = date('Y-m-d',strtotime($api_end_date.'+1 day'));

                $result = $wxObj->getDatacube('article','read',$api_start_date,$api_end_date);

                if(is_array($result)){
                    foreach ($result as $rs) {
                        $series[$item['id']]['data'][$rs['ref_date']][] = $rs;
                    }
                }

            }
            if($other_count>1){
                $api_start_date = $now_start_date;
                $api_end_date = date('Y-m-d',strtotime($now_start_date.'+'.($other_count-1).' days'));
                $result = $wxObj->getDatacube('article','read',$api_start_date,$api_end_date);

                if(isset($result) && is_array($result)){
                    foreach ($result as $rs) {
                        $series[$item['id']]['data'][$rs['ref_date']][] = $rs;
                    }
                }
            }
            }


        $series1 = array();

        foreach ($series as $k=>$item) {
            $series_item = array(
                'name'=>$item['name'],
                'type'=>'line',
                'stack'=>'总量',
                'data'=>array()
            );
            foreach ($item['data'] as $datums) {
                $num = 0;
                if($datums){
                    $num = 0;
                    foreach ($datums as $datum) {
                        $num += $datum['int_page_read_user'];
                    }

                }
                $series_item['data'][] = $num;

            }
            $series1[] = $series_item;
        }



        $this->assign('total_title','图文阅读数统计');
        $this->assign('sh',$sh);
        $this->assign('legend',json_encode($legend));
        $this->assign('xAxis',json_encode($allxAxis));
        $this->assign('series',json_encode($series1));
        return view('readstatistics');
    }

    public function apiStatistics(){
        $max_date = date('Y-m-d',strtotime('-1 day'));
        $this->assign('max_date',$max_date);
        if (Request::isGet() ) {
            $_data = input('get.');
           if(isset($_data['times']) && $_data['times']){
                $times = explode('到', $_data['times']);
                $start_date = trim($times[0]);
                $end_date = trim($times[1]);
                if(strtotime($end_date) > strtotime(date('Y-m-d'))){
                    $end_date = date('Y-m-d',strtotime('-1 day'));
                }
                  if(strtotime($start_date) > strtotime($end_date)){
                    $start_date = $end_date;
                }
               if((intval(strtotime($end_date) - strtotime($start_date))/86400) > 59){
                    $start_date = date('Y-m-d',strtotime($end_date.'-59 days'));
                }
            }else{
                $end_date = date('Y-m-d',strtotime('-1 day'));
                $start_date = date('Y-m-d',strtotime($end_date.'-13 days'));

            }
        }else{
            $end_date = date('Y-m-d',strtotime('-1 day'));
            $start_date = date('Y-m-d',strtotime($end_date.'-13 days'));

        }
        $times = $start_date." 到 ".$end_date;
        $sh['times']= $times;
        $mpList = $this->getRoleMpList();
        $legend = array();
        foreach ($mpList as $k=>$item) {
            $legend[] = $item['name'];
        }

        $end_time = strtotime($end_date);
        $start_time = strtotime($start_date);
        $allxAxis = array();
        $series = array();

        $date_count = 0;

        while ($start_time<=$end_time){
            $date_count++;
            $date = date('Y-m-d',$start_time);
            $allxAxis[] = $date;
            $start_time = $start_time + 86400;

        }

        $time_region = 30;
        $default_data = array();
        foreach ($allxAxis as $allxAxi) {
            $default_data[$allxAxi] = array();
        }
        foreach ($mpList as $item) {
            $region_count = intval($date_count/$time_region);
            $other_count = intval($date_count%$time_region);
            $now_start_date = $start_date;

            $wxObj = getWechatActiveObj($item['id']);

            $series[$item['id']] = array(
                'name'=>$item['name'],
                'type'=>'line',
                'stack'=>'总量',
                'data'=>$default_data
            );


            for(;$region_count>0;$region_count--){
                $api_start_date = $now_start_date;
                $api_end_date = date('Y-m-d',strtotime($now_start_date.'+'.($time_region-1).' days'));
                $now_start_date = date('Y-m-d',strtotime($api_end_date.'+1 day'));
                $result = $wxObj->getDatacube('interface','summary',$api_start_date,$api_end_date);

                if(is_array($result)){
                    foreach ($result as $rs) {
                        $series[$item['id']]['data'][$rs['ref_date']][] = $rs;
                    }
                }

            }

            if($other_count>1){
                $api_start_date = $now_start_date;
                $api_end_date = date('Y-m-d',strtotime($now_start_date.'+'.($other_count-1).' days'));
                $result = $wxObj->getDatacube('interface','summary',$api_start_date,$api_end_date);

                if(isset($result) && is_array($result)){
                    foreach ($result as $rs) {
                        $series[$item['id']]['data'][$rs['ref_date']][] = $rs;
                    }
                }
            }

        }

        $series1 = array();
        foreach ($series as $k=>$item) {
            $series_item = array(
                'name'=>$item['name'],
                'type'=>'line',
                'stack'=>'总量',
                'data'=>array()
            );
            foreach ($item['data'] as $datums) {
                $num = 0;
                if($datums){
                    $num = 0;
                    foreach ($datums as $datum) {
                        $num += $datum['callback_count'];
                    }

                }
                $series_item['data'][] = $num;

            }
            $series1[] = $series_item;
        }

        $this->assign('total_title','接口调用次数统计');
        $this->assign('sh',$sh);
        $this->assign('legend',json_encode($legend));
        $this->assign('xAxis',json_encode($allxAxis));
        $this->assign('series',json_encode($series1));
        return view('apistatistics');
    }

    public function messageStatistics(){
        $max_date = date('Y-m-d',strtotime('-1 day'));
        $this->assign('max_date',$max_date);
        if (Request::isGet() ) {
            $_data = input('get.');
           if(isset($_data['times']) && $_data['times']){
                $times = explode('到', $_data['times']);
                $start_date = trim($times[0]);
                $end_date = trim($times[1]);
                if(strtotime($end_date) > strtotime(date('Y-m-d'))){
                    $end_date = date('Y-m-d',strtotime('-1 day'));
                }
                  if(strtotime($start_date) > strtotime($end_date)){
                    $start_date = $end_date;
                }
               if((intval(strtotime($end_date) - strtotime($start_date))/86400) > 59){
                    $start_date = date('Y-m-d',strtotime($end_date.'-59 days'));
                }

            }else{
                $end_date = date('Y-m-d',strtotime('-1 day'));
                $start_date = date('Y-m-d',strtotime($end_date.'-13 days'));

            }
        }else{
            $end_date = date('Y-m-d',strtotime('-1 day'));
            $start_date = date('Y-m-d',strtotime($end_date.'-13 days'));

        }
        $times = $start_date." 到 ".$end_date;
        $sh['times']= $times;
        $mpList = $this->getRoleMpList();
        $legend = array();
        foreach ($mpList as $k=>$item) {
            $legend[] = $item['name'];
        }

        $end_time = strtotime($end_date);
        $start_time = strtotime($start_date);
        $allxAxis = array();
        $series = array();

        $date_count = 0;

        while ($start_time<=$end_time){
            $date_count++;
            $date = date('Y-m-d',$start_time);
            $allxAxis[] = $date;
            $start_time = $start_time + 86400;

        }

        $time_region = 7;
        $default_data = array();
        foreach ($allxAxis as $allxAxi) {
            $default_data[$allxAxi] = array();
        }
        foreach ($mpList as $item) {
            $region_count = intval($date_count/$time_region);
            $other_count = intval($date_count%$time_region);
            $now_start_date = $start_date;

            $wxObj = getWechatActiveObj($item['id']);

            $series[$item['id']] = array(
                'name'=>$item['name'],
                'type'=>'line',
                'stack'=>'总量',
                'data'=>$default_data
            );


            for(;$region_count>0;$region_count--){
                $api_start_date = $now_start_date;
                $api_end_date = date('Y-m-d',strtotime($now_start_date.'+'.($time_region-1).' days'));
                $now_start_date = date('Y-m-d',strtotime($api_end_date.'+1 day'));

                $result = $wxObj->getDatacube('upstreammsg','summary',$api_start_date,$api_end_date);

                if(is_array($result)){
                    foreach ($result as $rs) {
                        $series[$item['id']]['data'][$rs['ref_date']][] = $rs;
                    }
                }

            }

            if($other_count>1){
                $api_start_date = $now_start_date;
                $api_end_date = date('Y-m-d',strtotime($now_start_date.'+'.($other_count-1).' days'));
                $result = $wxObj->getDatacube('upstreammsg','summary',$api_start_date,$api_end_date);

                if(isset($result) && is_array($result)){
                    foreach ($result as $rs) {
                        $series[$item['id']]['data'][$rs['ref_date']][] = $rs;
                    }
                }
            }

            if(isset($result) && is_array($result)){
                foreach ($result as $rs) {
                    $series[$item['id']]['data'][$rs['ref_date']][] = $rs;
                }
            }
        }

        $series1 = array();

        foreach ($series as $k=>$item) {
            $series_item = array(
                'name'=>$item['name'],
                'type'=>'line',
                'stack'=>'总量',
                'data'=>array()
            );
            foreach ($item['data'] as $datums) {
                $num = 0;
                if($datums){
                    $num = 0;
                    foreach ($datums as $datum) {
                        $num += $datum['msg_user'];
                    }

                }
                $series_item['data'][] = $num;

            }
            $series1[] = $series_item;
        }



        $this->assign('total_title','消息发送人数统计');
        $this->assign('sh',$sh);
        $this->assign('legend',json_encode($legend));
        $this->assign('xAxis',json_encode($allxAxis));
        $this->assign('series',json_encode($series1));
        return view('messagestatistics');
    }
}